﻿using System;

namespace FDEE.Model
{
    /// <summary>
    /// 所有钢筋的基类
    /// </summary>
    public abstract class RebarBase : IComparable<RebarBase>
    {
        /// <summary>
        /// 主编号
        /// </summary>
        public int MBarMark;
        /// <summary>
        /// 次编号
        /// </summary>
        public int SBarMark;
        /// <summary>
        /// 后缀编号
        /// </summary>
        public string CBarMark;
        /// <summary>
        /// 直径
        /// </summary>
        public eDB Diameter;
        /// <summary>
        /// 数量
        /// </summary>
        public int Quantity;
        /// <summary>
        /// 大样
        /// </summary>
        public eShapeType ShapeConfg;
        /// <summary>
        /// 公式
        /// </summary>
        public string Formula;
        /// <summary>
        /// 长度
        /// </summary>
        public double Length;

        public int Radius, d, r, h, n, Lx;
        public double  A, B, C, D, E, F, R;

        //扩展范围
        public EXNumber EXNumberList;
        /// <summary>
        /// 单位重量
        /// </summary>
        public double UnitWeight { get { return GlobalFuncs.GetUnitWeight(Diameter); } }
        /// <summary>
        /// 总长
        /// </summary>
        public double TotalLength { get { return Math.Round(Quantity * Length, 3, MidpointRounding.AwayFromZero); } }
        /// <summary>
        /// 总重量
        /// </summary>
        public double TotalWeight { get { return Math.Round(TotalLength * UnitWeight, 3, MidpointRounding.AwayFromZero); } }

        ///// <summary>
        ///// 构造函数
        ///// </summary>
        ///// <param name="mBarMark"></param>
        ///// <param name="sBarMark"></param>
        ///// <param name="cBarMark"></param>
        ///// <param name="diameter"></param>
        ///// <param name="quantity"></param>
        ///// <param name="shapeConfg"></param>
        //public RebarBase(int mBarMark, int sBarMark, string cBarMark, eDB diameter, int quantity, eShapeType shapeConfg)
        //{
        //    MBarMark = mBarMark;
        //    SBarMark = sBarMark;
        //    CBarMark = cBarMark;
        //    Diameter = diameter;
        //    Quantity = quantity;
        //    ShapeConfg = shapeConfg;
        //    Length = 0;
        //}



        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mBarMark"></param>
        /// <param name="sBarMark"></param>
        /// <param name="cBarMark"></param>
        /// <param name="diameter"></param>
        /// <param name="quantity"></param>
        /// <param name="shapeConfg"></param>
        public RebarBase(int mBarMark, int sBarMark, string cBarMark, eDB diameter, int quantity, eShapeType shapeConfg, EXNumber ex = null)
        {
            MBarMark = mBarMark;
            SBarMark = sBarMark;
            CBarMark = cBarMark;
            Diameter = diameter;
            Quantity = quantity;
            ShapeConfg = shapeConfg;
            Length = 0;
            if (ex != null)
            {
                EXNumberList = ex;
            }
            else
            {
                EXNumberList = new EXNumber();
            }

        }

        /// <summary>
        /// 设置长度算法，所有子类必须实现。
        /// </summary>
        public abstract void SetLength();

        /// <summary>
        /// 重写基类
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            if (MBarMark == 0)
            {
                return base.ToString();

            }
            string m = MBarMark.ToString();
            if (SBarMark != 0)
            {
                m += "-";
                m += SBarMark.ToString();
            }
            m += CBarMark;
            m += "-";
            m += Quantity.ToString();
            m += "-";
            m += Length.ToString();            
            return m;
        }
        /// <summary>
        /// 实现钢筋编号比较
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public int CompareTo(RebarBase other)
        {
            return (this.MBarMark * 10 + this.SBarMark).CompareTo(other.MBarMark * 10 + other.SBarMark);
        }
    }


    /// <summary>
    /// AASHTO 钢筋大样常规参数，包含标准弯钩规则
    /// </summary>
    public static class RebarKnowledge
    {
        /// <summary>
        /// 根据钢筋直径获得弯钩内半径r
        /// </summary>
        /// <param name="diameter"></param>
        /// <returns></returns>
        internal static int GerRadius(eDB diameter)
        {
            int d = Convert.ToInt32(diameter);
            if (d <= 25)
            {
                return 3 * d;
            }
            else if (d == 28 || d == 32)
            {
                return 4 * d;
            }
            else
            {
                throw new Exception("钢筋直径未定义");
            }
        }

        /// <summary>
        /// 根据钢筋直径确定箍筋Lx
        /// </summary>
        /// <param name="diameter"></param>
        /// <returns></returns>
        internal static int GetLx(eDB diameter)
        {
            int d = Convert.ToInt32(diameter);
            if (d <= 10)
            {
                return 170;
            }
            else if (d <= 16)
            {
                return 16 * d;
            }
            else
            {
                return 12 * d;
            }
        }
        internal static int GetLxNew(eDB diameter)
        {
            int d = Convert.ToInt32(diameter);
            if (d <= 10)
            {
                return 170;
            }
            else if (d <= 12)
            {
                return 288;
            }
            else if (d <= 16)
            {
                return 24 * d;
            }
            else
            {
                return 24 * d;
            }
        }
        /// <summary>
        /// 根据钢筋直径确定箍筋内半径r
        /// </summary>
        /// <param name="diameter"></param>
        /// <returns></returns>
        internal static int GetRadiusForSrirrup(eDB diameter)
        {
            int d = Convert.ToInt32(diameter);
            if (d <= 16)
            {
                return 2 * d;
            }
            else
            {
                return 3 * d;
            }
        }
    }

}
